<?php

class typeModel extends Model
{

	public static function getById ( $id )
	{

		$db = &DB::getInstance ( require './config/db_config.php' ) ;

		$params = array (
				'id' => array (
						'value' => intval ( $id ),
						'type' => PDO::PARAM_INT
				)
		) ;
			
		$db->query ( 'SELECT * FROM type WHERE type_id=:id', $params ) ;
			
		$result = $db->fetchAll ( ) ;

		if ( count ( $result ) === 0 )
			return null ;
		else
			return new typeModel ( $result[0], true ) ;

	}
	
	public static function getAll ( )
	{
	
		$db = &DB::getInstance ( require './config/db_config.php' ) ;
	
		$db->query ( 'SELECT * FROM type' ) ;
			
		$result = $db->fetchAll ( ) ;
	
		if ( count ( $result ) === 0 )
			return null ;
		else
		{
				
			$res = array ( ) ;
				
			foreach ( $result as $type )
				$res[] = new typeModel ( $type, true ) ;
				
			return $res ;
				
		}
	
	}

	public function save ( )
	{
		
		if ( ! isset ( $this->data['type_intitule'] ) && ! isset ( $this->data['type_plafond'] ) && ! isset ( $this->data['type_retirer'] ) )
			throw new Exception ( 'Tous les champs du modèle ne sont pas renseignés.' ) ;
		elseif ( ! $this->already_saved )
		{
				
			$params = array (
					'entitled' => array (
							'value' => strval ( $this->data['type_intitule'] ),
							'type' => PDO::PARAM_STR
					),
					'ceiling' => array (
							'value' => ( $this->data['type_plafond'] === null ) ? null : intval ( $this->data['type_plafond'] ),
							'type' => ( $this->data['type_plafond'] === null ) ? PDO::PARAM_NULL : PDO::PARAM_INT
					),
					'withdraw' => array (
							'value' => ( $this->data['type_retirer'] ) ? '1' : '0',
							'type' => PDO::PARAM_INT
					),
					'overdraft' => array (
							'value' => ( $this->data['type_decouvert'] === null ) ? null : intval ( $this->data['type_decouvert'] ),
							'type' => ( $this->data['type_decouvert'] === null ) ? PDO::PARAM_NULL : PDO::PARAM_INT
					)
			) ;

			$this->db->query ( 'INSERT INTO Type (type_intitule, type_plafond, type_retirer, type_decouvert) VALUES ( :entitled, :ceiling, :withdraw, :overdraft )', $params ) ;
			$this->data['type_id'] = $this->db->lastId ( ) ;
				
			$this->already_saved = true ;
			$this->modified = false ;
				
		}
		else if ( $this->modified )
		{
				
			$params = array (
					'id' => array (
							'value' => strval ( $this->data['type_id'] ),
							'type' => PDO::PARAM_INT
					),
					'entitled' => array (
							'value' => strval ( $this->data['type_intitule'] ),
							'type' => PDO::PARAM_STR
					),
					'ceiling' => array (
							'value' => ( $this->data['type_plafond'] === null ) ? null : intval ( $this->data['type_plafond'] ),
							'type' => ( $this->data['type_plafond'] === null ) ? PDO::PARAM_NULL : PDO::PARAM_INT
					),
					'withdraw' => array (
							'value' => ( $this->data['type_retirer'] ) ? '1' : '0',
							'type' => PDO::PARAM_INT
					),
					'overdraft' => array (
							'value' => ( $this->data['type_decouvert'] === null ) ? null : intval ( $this->data['type_decouvert'] ),
							'type' => ( $this->data['type_decouvert'] === null ) ? PDO::PARAM_NULL : PDO::PARAM_INT
					)
			) ;
				
			$this->db->query ( 'UPDATE Type SET type_intitule=:entitled, type_plafond=:ceiling, type_retirer=:withdraw, type_decouvert=:overdraft WHERE type_id=:id', $params ) ;
				
			$this->modified = false ;
				
		}

	}

	public function delete ( )
	{

		if ( $this->already_saved )
		{
			
			$params = array (
					'id' => array (
							'value' => intval ( $this->data['type_id'] ),
							'type' => PDO::PARAM_INT
					)
			) ;
			
			$this->db->query ( 'DELETE FROM Type WHERE type_id=:id', $params ) ;
			
			$this->already_saved = false ;
			
		}

	}

	public function getId ( ) { return $this->data['type_id'] ; }
	public function getEntitled ( ) { return $this->data['type_intitule'] ; }
	public function getCeiling ( ) { return $this->data['type_plafond'] ; }
	public function getWithdraw ( ) { return $this->data['type_retirer'] ; }
	public function getOverdraft ( ) { return $this->data['type_decouvert'] ; }
	
	public function setEntitled ( $arg ) { $this->data['type_intitule'] = $arg ; $this->modified = true ; }
	public function setCeiling ( $arg ) { $this->data['type_plafond'] = $arg ; $this->modified = true ; }
	public function setWithdraw ( $arg ) { $this->data['type_retirer'] = $arg ; $this->modified = true ; }
	public function setOverdraft ( $arg ) { $this->data['type_decouvert'] = $arg ; $this->modified = true ; }
	
}

?>